package com.logos.musicframe.utils;

import java.util.Random;

/**
 * @author logos
 * date 2025/6/01 22:19
 * @version v1.0
 * @Package com.logos.musicframe.utils
 */
public class RandomStringUtil {
    private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String NUMBERS = "0123456789";
    private static final Random random = new Random();

    /**
     * 随机获取指定长度的英文字母字符串（默认大小写字母）
     *
     * @param length 字符串长度
     * @return 随机字母字符串
     */
    public static String randomAlphabetic(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHABET.length());
            sb.append(ALPHABET.charAt(index));
        }
        return sb.toString();
    }

    // 快捷方法：直接获取 4 个字母
    public static String randomFourLetters() {
        return randomAlphabetic(4);
    }

    /**
     * 随机生成 4 位大写字母验证码
     */
    public static String generateCaptchaCode(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHABET.length());
            sb.append(ALPHABET.charAt(index));
        }
        return sb.toString();
    }

    // 快捷方法：生成 4 位验证码
    public static String getCaptchaCode() {
        return generateCaptchaCode(4);
    }
    
    /**
     * 随机生成指定长度的数字验证码
     *
     * @param length 验证码长度
     * @return 数字验证码
     */
    public static String generateNumericCode(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(NUMBERS.length());
            sb.append(NUMBERS.charAt(index));
        }
        return sb.toString();
    }
    
    /**
     * 生成6位数字验证码
     *
     * @return 6位数字验证码
     */
    public static String getSmsCode() {
        return generateNumericCode(6);
    }
}
